package controller;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Scanner;

import model.GameConstants;

import ctf.network.SocketClosedException;
import ctf.network.UninitializedSocketException;
import ctf.network.tcp.Connection;
import ctf.network.tcp.ConnectionOwner;
/**
 * A controller used to test the execution of the simulator.
 * @author N. Bhatia, A. Butt, N. Evans, C. Levesque, P. OKeefe 
 *
 */
public class TestController implements ConnectionOwner {
	private Connection connection;

	public TestController() {
		try {
			connection = new Connection("localhost",
					GameConstants.SERVER_PORT);
		} catch (UninitializedSocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		connection.setOwner(this);
		connection.start();

	}

	/**
	 * Reads input from the standard input
	 */
	public void getInput() {

		Scanner reader = new Scanner(System.in);

		while (true) {
			String message = reader.nextLine();
			try {
				connection.sendLine(message);
			} catch (SocketClosedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UninitializedSocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void connectionClosed(Connection arg0) {
		System.out.println("Connection closed");
	}

	@Override
	public void lineReceived(String arg0, Connection arg1) {
		System.out.println("Message received:" + arg0);
	}

}
